package nodosRegex;


import grafo.keyWord;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import cmdrunner.*;


public class Match {

	private File archivo = null;
	private FileReader fr = null;
	private BufferedReader br = null;
	ArrayList<String> a = new ArrayList<String>();
	ArrayList<String> a2 = new ArrayList<String>();
	ArrayList<PreNodo> a3 = new ArrayList<PreNodo>();
	PreNodo aux;
	String[] links;

	public static void main(String[] args) {
		new Match();
	}
	public Match() {
		String html;
		try {
			
			Main m= new Main();
			
			archivo = new File("historialdenavegacion.txt");
			
			fr = new FileReader(archivo);
			br = new BufferedReader(fr);
			String linea;
			while ((linea = br.readLine()) != null) {
				html = parseString(linea);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				if (null != fr) {
					fr.close();
					//archivo.delete();
					
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		int k = 0, umbral=2;
		int index=0;
		String tmp = "";
		for (String lista1 : a) {
			for (String lista2 : a) {
				if (lista1.equals(lista2)) {
					k++;
				}
			}
			if (k >= umbral){
				
				Collections.sort(a);
			    index = Collections.binarySearch(a2,lista1);
			    if(index<0){
			    	a2.add(lista1);
			    	aux=new PreNodo(lista1,k);
			    	a3.add(aux);

//			    	System.out.println("El link: " + lista1 + " se repite: " + k
//							+ " veces.");
			    }
			}
			tmp = lista1;
			k = 0;
		}
		//System.out.println("Lista: " + a3);
	}
	
	
	public String parseString(String linea) {
		Pattern p = Pattern.compile("^.*(http.*).*");
		Matcher m = p.matcher(linea);

		if (m.find()) {
			a.add(new String(m.group(1)));
//			System.out.println(m.group(1));
		}
		return "";
	}
	public ArrayList<String> getA2() {
		return a2;
	}
	public void setA2(ArrayList<String> a2) {
		this.a2 = a2;
	}
	public ArrayList<PreNodo> getA3() {
		return a3;
	}
	public void setA3(ArrayList<PreNodo> a3) {
		this.a3 = a3;
	}
}